import request from '@/utils/network/request'
import NProgress from 'nprogress'
import message from 'ant-design-vue/es/message'
// import { closeMyLoading } from './pageInitFn'

const showError = (msg) => {
  console.error('message', msg)
  message.error('系统异常,请联系管理员')
}
const Dhttp = {
  get (url, params) {
    NProgress.start()
    NProgress.inc()
    return new Promise((resolve, reject) => {
      request({
        method: 'get',
        url: url,
        params: params
      }).then(res => {
        // console.log('get')
        // console.log(url)
        // console.log(22)
        // console.log(res)
        // eslint-disable-next-line eqeqeq
        if (res && res.success === false && res.message) {
          // closeMyLoading()
          showError(res.message)
        } else {
          resolve(res)
        }
        NProgress.done()
      }).catch((msg) => {
        NProgress.done()
      })
    })
  },
  post (url, params, data) {
    NProgress.start()
    NProgress.inc()
    return new Promise((resolve, reject) => {
      request({
        method: 'post',
        url: url,
        data: data,
        params: params
      }).then(res => {
        if (res && res.success === false && res.message) {
          // closeMyLoading()
          showError(res.message)
          reject(res.message)
        } else {
          resolve(res)
        }
        NProgress.done()
      }).catch((msg) => {
        NProgress.done()
        reject(msg)
      })
    })
  },
  postxf (url, data) {
    NProgress.start()
    NProgress.inc()

    const config = {
      method: 'post',
      url: url,
      data: data,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      }
    }
    return new Promise((resolve, reject) => {
      request(config).then(res => {
        // eslint-disable-next-line eqeqeq
        if (res && res.success == false && res.message) {
          // closeMyLoading()
          message.error(res.message)
        } else {
          resolve(res)
        }
        NProgress.done()
      }).catch((msg) => {
        NProgress.done()
      })
    })
  }
}

export default {
  get: Dhttp.get,
  post: Dhttp.post,
  postxf: Dhttp.postxf
}
